package com.wave.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.net.NetUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wave.common.core.domain.PageQuery;
import com.wave.common.core.page.TableDataInfo;
import com.wave.common.utils.StringUtils;
import com.wave.system.domain.SysSetting;
import com.wave.system.domain.bo.SysSettingBo;
import com.wave.system.domain.vo.SysSettingVo;
import com.wave.system.mapper.SysSettingMapper;
import com.wave.system.service.ISysSettingService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 系统设置Service业务层处理
 *
 * @author wave
 * @date 2024-10-15
 */
@RequiredArgsConstructor
@Service
public class SysSettingServiceImpl implements ISysSettingService {

    private final SysSettingMapper baseMapper;

    /**
     * 查询系统设置
     */
    @Override
    public SysSettingVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询系统设置列表
     */
    @Override
    public TableDataInfo<SysSettingVo> queryPageList(SysSettingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSetting> lqw = buildQueryWrapper(bo);
        Page<SysSettingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询系统设置列表
     */
    @Override
    public List<SysSettingVo> queryList(SysSettingBo bo) {
        LambdaQueryWrapper<SysSetting> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSetting> buildQueryWrapper(SysSettingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSetting> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWmsAddress()), SysSetting::getWmsAddress, bo.getWmsAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getMesAddress()), SysSetting::getMesAddress, bo.getMesAddress());
        lqw.eq(bo.getAgvSetting() != null, SysSetting::getAgvSetting, bo.getAgvSetting());
        lqw.eq(StringUtils.isNotBlank(bo.getDayShiftStart()), SysSetting::getDayShiftStart, bo.getDayShiftStart());
        lqw.eq(StringUtils.isNotBlank(bo.getDayShiftEnd()), SysSetting::getDayShiftEnd, bo.getDayShiftEnd());
        lqw.eq(StringUtils.isNotBlank(bo.getNightShiftStart()), SysSetting::getNightShiftStart, bo.getNightShiftStart());
        lqw.eq(StringUtils.isNotBlank(bo.getNightShiftEnd()), SysSetting::getNightShiftEnd, bo.getNightShiftEnd());
        lqw.eq(bo.getCleanSetting() != null, SysSetting::getCleanSetting, bo.getCleanSetting());
        lqw.eq(bo.getCleanHistoryMonth() != null, SysSetting::getCleanHistoryMonth, bo.getCleanHistoryMonth());
        return lqw;
    }

    /**
     * 新增系统设置
     */
    @Override
    public Boolean insertByBo(SysSettingBo bo) {
        SysSetting add = BeanUtil.toBean(bo, SysSetting.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改系统设置
     */
    @Override
    public Boolean updateByBo(SysSettingBo bo) {
        SysSetting update = BeanUtil.toBean(bo, SysSetting.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysSetting entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除系统设置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean checkAddress(String address) {
        String[] arrays = address.split(":");
        if (arrays.length != 2) {
            return false;
        }
        String ip = arrays[0];
        String port = arrays[1];
        boolean checkResult = NetUtil.ping(ip);
        if (!checkResult) {
            return false;
        }
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(ip, Integer.parseInt(port)), 3000);
        } catch (IOException e) {
            return false;
        }
        return true;
    }
}
